package com.mathassistant.server.domain;

import java.util.ArrayList;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.listener.StoreCallback;

import com.mathassistant.shared.ExerciseDTO;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Exercise implements StoreCallback{
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
	private String key;
	
	@Persistent(serialized = "true")
	private ArrayList<String> question = new ArrayList<String>();
	
	@Persistent(serialized = "true")
	private ArrayList<String> chooses = new ArrayList<String>();
	
	@Persistent
	private int indexAnswer;
	
	@Persistent
	private Topic topicExercise;

	public Exercise(){}
	
	public Exercise(String question, int indexAnswer, ArrayList<String> chooses){
		this.setChooses(chooses);
		this.setQuestion(convertString2ArrayList(question));
		this.setIndexAnswer(indexAnswer);
	}
	
	public Exercise(ExerciseDTO dto){
		this.setQuestion(convertString2ArrayList(dto.getQuestion()));
		this.setIndexAnswer(dto.getAnswer());
		this.setChooses(dto.getChooses());
	}
	
	public String getKey()
	{
		return key;
	}	
	public void setQuestion(ArrayList<String> question) {
		this.question = question;
	}

	public void setChooses(ArrayList<String> chooses) {
		this.chooses = chooses;
	}

	public void setIndexAnswer(int answer) {
		this.indexAnswer = answer;
	}

	public int getIndexAnswer() {
		return indexAnswer;
	}

	public ArrayList<String> getChooses() {
		return chooses;
	}

	public String getQuestion() {
		String t = "";
		for(String i : this.question){
			t += i;
		}
		return t;
	}

	public ExerciseDTO toDTO(){
		return new ExerciseDTO(this.getKey(),this.getQuestion().toString(),this.getChooses(),this.getIndexAnswer());
	}
	
	@Override
	public void jdoPreStore() {
		// TODO Auto-generated method stub
	}

	public void updateformDTO(ExerciseDTO exercise) {
		// TODO Auto-generated method stub
		this.setIndexAnswer(exercise.getAnswer());
		this.setQuestion(convertString2ArrayList(exercise.getQuestion()));
		this.setChooses(exercise.getChooses());
	}
	
	public ArrayList<String> convertString2ArrayList(String convert){
		ArrayList<String> result = new ArrayList<String>();
		int length = convert.length();
		int i = 0;
		do{
			length -= 450;
			if(length <= 0){
				result.add(convert.substring(i));
				return result;
			}
			String t = convert.substring(i, i + 449);
			result.add(t);
			i += 450;
		} while (length > 0);
		return result;
	}

	public void setTopicExercise(Topic topicExercise) {
		this.topicExercise = topicExercise;
	}

	public Topic getTopicExercise() {
		return topicExercise;
	}
	
}
